package com.walkline.weibo.dao;

import java.util.Date;
import net.rim.device.api.i18n.SimpleDateFormat;
import org.json.me.JSONObject;
import com.walkline.util.StringUtility;
import com.walkline.weibo.WeiboException;
import com.walkline.weibo.WeiboSDK;
import com.walkline.weibo.inf.Comment;
import com.walkline.weibo.inf.Source;
import com.walkline.weibo.inf.Status;
import com.walkline.weibo.inf.User;

public class WeiboComment extends WeiboObject implements Comment
{
	private Date _created_at = new Date();
	private long _id = 0;
	private String _mid = "";
	private String _id_str = "";
	private String _text = "";
	private Source _source = null;
	private User _user = null;
	private Status _status = null;
	private Comment _reply_comment = null;

	public WeiboComment(WeiboSDK weibo, JSONObject jsonObject)	throws WeiboException 
	{
		super(weibo, jsonObject);

		JSONObject status = jsonObject;
		if (status != null)
		{
			_created_at = StringUtility.parseDate(status.optString("created_at"));
			_id = status.optLong("id");
			_mid = status.optString("mid");
			_id_str = status.optString("idstr");
			_text = status.optString("text");

			if (!status.isNull("source") && !status.optString("source").equals("")) {_source = new Source(status.optString("source"));}

			if (!status.isNull("user")) {_user = new WeiboUser(weibo, status.optJSONObject("user"));}

			if (!status.isNull("status")) {_status = new WeiboStatus(weibo, status.optJSONObject("status"));}

			if (!status.isNull("reply_comment")) {_reply_comment = new WeiboComment(weibo, status.optJSONObject("reply_comment"));}
		}
	}

	/**
	 * 微博创建时间 (长日期)
	 * @return 格式为: "yyyy-MM-dd HH:mm"
	 */
	public String getCreatedAtLong()
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		return sdf.formatLocal(_created_at.getTime());
	}

	/**
	 * 微博创建时间 (短日期)
	 * @return 格式为: "M/d HH:mm"
	 */
	public String getCreatedAtShort()
	{
		SimpleDateFormat sdf = new SimpleDateFormat("M/d HH:mm");

		return sdf.formatLocal(_created_at.getTime());
	}

	/**
	 * 微博ID
	 */
	public long getId() {return _id;}

	/**
	 * 微博MID
	 */
	public String getMid() {return _mid;}

	/**
	 * 字符串型的微博ID
	 */
	public String getIdStr() {return _id_str;}

	/**
	 * 微博信息内容
	 */
	public String getText() {return _text;}

	/**
	 * 微博来源
	 */
	public Source getSource() {return _source;}

	/**
	 * 微博作者的用户信息字段
	 */
	public User getUser() {return _user;}

	/**
	 * 被转发的原微博信息字段，当该微博为转发微博时返回
	 */
	public Status getStatus() {return _status;}

	/**
	 * 评论来源评论，当本评论属于对另一评论的回复时返回此字段
	 */
	public Comment getReplyComment() {return _reply_comment;}
}